package org.dromara.commission.listener;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.metadata.CellExtra;
import lombok.extern.slf4j.Slf4j;
import org.dromara.commission.domain.bo.LzshAgentLatitudeBo;
import org.dromara.commission.domain.vo.LzshAgentLatitudeImportVo;
import org.dromara.commission.domain.vo.LzshAgentLatitudeVo;
import org.dromara.commission.service.ILzshAgentLatitudeService;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.utils.SpringUtils;
import org.dromara.common.core.utils.ValidatorUtils;
import org.dromara.common.excel.core.ExcelListener;
import org.dromara.common.excel.core.ExcelResult;
import org.dromara.common.satoken.utils.LoginHelper;


import java.util.List;

/**
 * 系统用户自定义导入
 *
 * @author Lion Li
 */
@Slf4j
public class LzshAgentLatitudeImportListener extends AnalysisEventListener<LzshAgentLatitudeImportVo> implements ExcelListener<LzshAgentLatitudeImportVo> {

    private final ILzshAgentLatitudeService lzshAgentLatitudeService;

    private final Boolean isUpdateSupport;

    private final Long operUserId;

    private int successNum = 0;
    private int failureNum = 0;
    private final StringBuilder successMsg = new StringBuilder();
    private final StringBuilder failureMsg = new StringBuilder();

    public LzshAgentLatitudeImportListener(Boolean isUpdateSupport) {
        this.lzshAgentLatitudeService = SpringUtils.getBean(ILzshAgentLatitudeService.class);;
        this.isUpdateSupport = isUpdateSupport;
        this.operUserId = LoginHelper.getUserId();
    }

    @Override
    public void invoke(LzshAgentLatitudeImportVo lzshAgentLatitudeVo, AnalysisContext context) {
        LzshAgentLatitudeVo lzshAgentLatitude = this.lzshAgentLatitudeService.queryByBusinessCode(lzshAgentLatitudeVo.getBusinessCode());
        try {
            // 验证是否存在这个代理商维度记录
            if (ObjectUtil.isNull(lzshAgentLatitude)) {
                //不存在
                LzshAgentLatitudeBo agentLatitude = BeanUtil.toBean(lzshAgentLatitudeVo, LzshAgentLatitudeBo.class);
                ValidatorUtils.validate(agentLatitude);
                agentLatitude.setCreateBy(operUserId);
                lzshAgentLatitudeService.insertByBo(agentLatitude);
                successNum++;
                successMsg.append("<br/>").append(successNum).append("、代理商 ").append(agentLatitude.getBusinessCode()).append(" 导入成功");
            } else if (isUpdateSupport) {
                Long lzshAgentLatitudeId = lzshAgentLatitude.getId();
                LzshAgentLatitudeBo agentLatitude = BeanUtil.toBean(lzshAgentLatitudeVo, LzshAgentLatitudeBo.class);
                agentLatitude.setId(lzshAgentLatitudeId);
                agentLatitude.setUpdateBy(operUserId);
                lzshAgentLatitudeService.updateByBo(agentLatitude);
                successNum++;
                successMsg.append("<br/>").append(successNum).append("、代理商 ").append(agentLatitude.getBusinessCode()).append(" 更新成功");
            } else {
                failureNum++;
                failureMsg.append("<br/>").append(failureNum).append("、代理商 ").append(lzshAgentLatitude.getBusinessCode()).append(" 已存在");
            }
        } catch (Exception e) {
            failureNum++;
            String msg = "<br/>" + failureNum + "、代理商 " + lzshAgentLatitudeVo.getBusinessCode() + " 导入失败：";
            failureMsg.append(msg).append(e.getMessage());
            log.error(msg, e);
        }
    }

    @Override
    public void onException(Exception exception, AnalysisContext context) throws Exception {
        super.onException(exception, context);
    }

    @Override
    public void extra(CellExtra extra, AnalysisContext context) {
        super.extra(extra, context);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {

    }

    @Override
    public boolean hasNext(AnalysisContext context) {
        return super.hasNext(context);
    }

    @Override
    public ExcelResult<LzshAgentLatitudeImportVo> getExcelResult() {
        return new ExcelResult<>() {

            @Override
            public String getAnalysis() {
                if (failureNum > 0) {
                    failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
                    throw new ServiceException(failureMsg.toString());
                } else {
                    successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
                }
                return successMsg.toString();
            }

            @Override
            public List<LzshAgentLatitudeImportVo> getList() {
                return null;
            }

            @Override
            public List<String> getErrorList() {
                return null;
            }
        };
    }
}
